<!DOCTYPE html>
<html lang="zh-Hans">
<head>

    <!--[if lt IE 9]>
        <style>body {display: none; background: none !important} </style>
        <meta http-equiv="Refresh" Content="0; url=//outdatedbrowser.com/" />
    <![endif]-->

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<meta name="format-detection" content="telephone=no" />
<meta name="author" content="zhengweiMao" />



<meta property="og:type" content="article">
<meta property="og:title" content="14.并发编程--Concurrent-工具类介绍">
<meta property="og:url" content="https://maozw.gitee.io/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/14.并发编程--Concurrent-工具类/index.html">
<meta property="og:site_name" content="花神">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2018-08-06T15:26:04.017Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="14.并发编程--Concurrent-工具类介绍">

<link rel="apple-touch-icon" href= "/apple-touch-icon.png">


    <link rel="alternate" href="/atom.xml" title="花神" type="application/atom+xml">



    <link rel="shortcut icon" href="/favicon.png">



    <link href="//cdn.bootcss.com/animate.css/3.5.1/animate.min.css" rel="stylesheet">



    <link href="//cdn.bootcss.com/fancybox/2.1.5/jquery.fancybox.min.css" rel="stylesheet">



    <script src="//cdn.bootcss.com/pace/1.0.2/pace.min.js"></script>
    <link href="//cdn.bootcss.com/pace/1.0.2/themes/blue/pace-theme-minimal.css" rel="stylesheet">


<link rel="stylesheet" href="/maoge-blog/css/style.css">



<link href="//cdn.bootcss.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet">


<title>14.并发编程--Concurrent-工具类介绍 | 花神</title>

<script src="//cdn.bootcss.com/jquery/2.2.4/jquery.min.js"></script>
<script src="//cdn.bootcss.com/clipboard.js/1.5.10/clipboard.min.js"></script>

<script>
    var yiliaConfig = {
        fancybox: true,
        animate: true,
        isHome: false,
        isPost: true,
        isArchive: false,
        isTag: false,
        isCategory: false,
        fancybox_js: "//cdn.bootcss.com/fancybox/2.1.5/jquery.fancybox.min.js",
        scrollreveal: "//cdn.bootcss.com/scrollReveal.js/3.1.4/scrollreveal.min.js",
        search: true
    }
</script>


    <script>
        yiliaConfig.jquery_ui = [true, "//cdn.bootcss.com/jqueryui/1.10.4/jquery-ui.min.js", "//cdn.bootcss.com/jqueryui/1.10.4/css/jquery-ui.min.css"];
    </script>



    <script> yiliaConfig.rootUrl = "\/";</script>






</head>
<body>
  <div id="container">
    <div class="left-col">
    <div class="overlay"></div>
<div class="intrude-less">
    <header id="header" class="inner">
        <a href="/maoge-blog/" class="profilepic">
            <img src="img/avatar.png" class="animated zoomIn">
        </a>
        <hgroup>
          <h1 class="header-author"><a href="/maoge-blog/">zhengweiMao</a></h1>
        </hgroup>

        

        
            <form id="search-form">
            <input type="text" id="local-search-input" name="q" placeholder="search..." class="search form-control" autocomplete="off" autocorrect="off" searchonload="" />
            <i class="fa fa-times" onclick="resetSearch()"></i>
            </form>
            <div id="local-search-result"></div>
            <p class='no-result'>No results found <i class='fa fa-spinner fa-pulse'></i></p>
        


        
            <div id="switch-btn" class="switch-btn">
                <div class="icon">
                    <div class="icon-ctn">
                        <div class="icon-wrap icon-house" data-idx="0">
                            <div class="birdhouse"></div>
                            <div class="birdhouse_holes"></div>
                        </div>
                        <div class="icon-wrap icon-ribbon hide" data-idx="1">
                            <div class="ribbon"></div>
                        </div>
                        
                        <div class="icon-wrap icon-link hide" data-idx="2">
                            <div class="loopback_l"></div>
                            <div class="loopback_r"></div>
                        </div>
                        
                        
                        <div class="icon-wrap icon-me hide" data-idx="3">
                            <div class="user"></div>
                            <div class="shoulder"></div>
                        </div>
                        
                    </div>
                    
                </div>
                <div class="tips-box hide">
                    <div class="tips-arrow"></div>
                    <ul class="tips-inner">
                        <li>菜单</li>
                        <li>标签</li>
                        
                        <li>友情链接</li>
                        
                        
                        <li>关于我</li>
                        
                    </ul>
                </div>
            </div>
        

        <div id="switch-area" class="switch-area">
            <div class="switch-wrap">
                <section class="switch-part switch-part1">
                    <nav class="header-menu">
                        <ul>
                        
                            <li><a href="/maoge-blog/">主页</a></li>
                        
                            <li><a href="/maoge-blog/archives/">所有文章</a></li>
                        
                            <li><a href="/maoge-blog/tags/">标签云</a></li>
                        
                            <li><a href="/maoge-blog/categories/">分类</a></li>
                        
                            <li><a href="/maoge-blog/about/">关于我</a></li>
                        
                        </ul>
                    </nav>
                    <nav class="header-nav">
                        <ul class="social">
                            
                                <a class="fa Email" href="mailto:123@123.com" title="Email"></a>
                            
                                <a class="fa GitHub" href="#" title="GitHub"></a>
                            
                                <a class="fa RSS" href="/maoge-blog/atom.xml" title="RSS"></a>
                            
                        </ul>
                    </nav>
                </section>
                
                
                <section class="switch-part switch-part2">
                    <div class="widget tagcloud" id="js-tagcloud">
                        
                    </div>
                </section>
                
                
                
                <section class="switch-part switch-part3">
                    <div id="js-friends">
                    
                      <a class="main-nav-link switch-friends-link" href="https://hexo.io">Hexo</a>
                    
                      <a class="main-nav-link switch-friends-link" href="https://pages.github.com/">GitHub</a>
                    
                      <a class="main-nav-link switch-friends-link" href="http://moxfive.xyz/">MOxFIVE</a>
                    
                    </div>
                </section>
                

                
                
                <section class="switch-part switch-part4">
                
                    <div id="js-aboutme">专注于前端</div>
                </section>
                
            </div>
        </div>
    </header>                
</div>
    </div>
    <div class="mid-col">
      <nav id="mobile-nav">
      <div class="overlay">
          <div class="slider-trigger"></div>
          <h1 class="header-author js-mobile-header hide"><a href="/" title="回到主页">zhengweiMao</a></h1>
      </div>
    <div class="intrude-less">
        <header id="header" class="inner">
            <a href="/maoge-blog/" class="profilepic">
                <img src="img/avatar.png" class="animated zoomIn">
            </a>
            <hgroup>
              <h1 class="header-author"><a href="/maoge-blog/" title="回到主页">zhengweiMao</a></h1>
            </hgroup>
            
            <nav class="header-menu">
                <ul>
                
                    <li><a href="/maoge-blog/">主页</a></li>
                
                    <li><a href="/maoge-blog/archives/">所有文章</a></li>
                
                    <li><a href="/maoge-blog/tags/">标签云</a></li>
                
                    <li><a href="/maoge-blog/categories/">分类</a></li>
                
                    <li><a href="/maoge-blog/about/">关于我</a></li>
                
                <div class="clearfix"></div>
                </ul>
            </nav>
            <nav class="header-nav">
                        <ul class="social">
                            
                                <a class="fa Email" target="_blank" href="mailto:123@123.com" title="Email"></a>
                            
                                <a class="fa GitHub" target="_blank" href="#" title="GitHub"></a>
                            
                                <a class="fa RSS" target="_blank" href="/maoge-blog/atom.xml" title="RSS"></a>
                            
                        </ul>
            </nav>
        </header>                
    </div>
    <link class="menu-list" tags="标签" friends="友情链接" about="关于我"/>
</nav>
      <div class="body-wrap"><article id="post-花神(maoge)/1.并发编程/14.并发编程--Concurrent-工具类" class="article article-type-post" itemscope itemprop="blogPost">
  
    <div class="article-meta">
      <a href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/14.并发编程--Concurrent-工具类/" class="article-date">
      <time datetime="2018-07-05T06:26:57.000Z" itemprop="datePublished">2018-07-05</time>
</a>


    </div>
  
  <div class="article-inner">
    
      <input type="hidden" class="isFancy" />
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      14.并发编程--Concurrent-工具类介绍
    </h1>
  

      </header>
      
      <div class="article-info article-info-post">
        
    <div class="article-category tagcloud">
    <a class="article-category-link" href="/maoge-blog/categories/并发编程/">并发编程</a>
    </div>


        
        <div class="clearfix"></div>
      </div>
      
    
    <div class="article-entry" itemprop="articleBody">
      
          
        <p><excerpt in="" index="" |="" 首页摘要=""><br><a id="more"></a></excerpt></p>
<the rest="" of="" contents="" |="" 余下全文="">

<h3 id="并发编程–Concurrent-工具类介绍"><a href="#并发编程–Concurrent-工具类介绍" class="headerlink" title="并发编程–Concurrent-工具类介绍"></a>并发编程–Concurrent-工具类介绍</h3><p>前言</p>
<ul>
<li>Java 5 添加了一个新的包到 Java 平台，java.util.concurrent 包<br>这个包包含有一系列能够让 Java 的并发编程变得更加简单轻松的类。在这个包被添加以前，你需要自己去动手实现自己的相关工具类；</li>
<li>本文我将带你一一认识 java.util.concurrent 包里的这些类，然后你可以尝试着如何在项目中使用它们。本文中我将使用 Java 6 版本，我不确定这和 Java 5 版本里的是否有一些差异</li>
<li>我不会去解释关于 Java 并发的核心问题 - 其背后的原理，也就是说，如果你对那些东西感兴趣，参考《Java 并发指南》；</li>
</ul>
<h6 id="并发编程–Concurrent-工具类介绍-1"><a href="#并发编程–Concurrent-工具类介绍-1" class="headerlink" title="并发编程–Concurrent-工具类介绍"></a>并发编程–Concurrent-工具类介绍</h6><ul>
<li>CountDownLatch </li>
<li>CylicBarrier </li>
<li>Semaphore </li>
<li>Condition 对象监视器下个篇幅引入</li>
<li>Callable   不单独说明 Demo中会使用 </li>
<li>Future </li>
</ul>
<h3 id="1-CountDownLatch-计时器-简介"><a href="#1-CountDownLatch-计时器-简介" class="headerlink" title="1. CountDownLatch(计时器) 简介"></a>1. CountDownLatch(计时器) 简介<br></h3><p>  官方介绍</p>
<blockquote>
<p> A synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes.<br><br>      同步协助，允许一个或多个线程等待，直到其他一组线程执行的操作完成，再继续执行。<br><br>  理解：<br>      CountDownLatch是一个同步的辅助类，它可以允许一个或多个线程等待，直到一组在其它线程中的操作执行完成。<br><br>      一个CountDownLatch会通过一个给定的count数来被初始化。其中await()方法会一直阻塞，来作为一个开/关的门或门闩：直到当前的count被减到0，而这个过程是通过调用countDown()方法来实现的。在await()方法不再阻塞以后，所有等待的线程都会被释放，并且任何await()的子调用都会立刻返回。这是一次性的－－count不能被重置。如果你需要一种能重置count的版本，请考虑使用CyclicBarrier。<br><br>      CountDownLatch一个有用的属性就是它不需要线程们在继续执行之前，调用countDown来等待count被减到0。它简单地阻止了任何调用了await()的线程继续，直到所有的线程都能够通过<br></p>
</blockquote>
<h5 id="CountDownLatch-的作用。"><a href="#CountDownLatch-的作用。" class="headerlink" title="CountDownLatch 的作用。"></a>CountDownLatch 的作用。<br></h5><pre><code>个人理解：CountDownLatch:我把他理解成倒计时锁&lt;br /&gt;
场景：常用于监听某些初始化操作，等待初始化操作完成后，通知主线程继续工作
</code></pre><h4 id="示例：MyCountDownLatch-Java"><a href="#示例：MyCountDownLatch-Java" class="headerlink" title="示例：MyCountDownLatch.Java"></a>示例：MyCountDownLatch.Java</h4><pre><code><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyCountDownLatch</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">      <span class="keyword">final</span> CountDownLatch countDown = <span class="keyword">new</span> CountDownLatch(<span class="number">2</span>);</span><br><span class="line"></span><br><span class="line">      Thread t1 = <span class="keyword">new</span> Thread(<span class="keyword">new</span> Runnable() &#123;</span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">          <span class="keyword">try</span> &#123;</span><br><span class="line">            System.out.println(<span class="string">"进入线程t1"</span> + <span class="string">"等待其他线程处理完成..."</span>);</span><br><span class="line">            countDown.await();</span><br><span class="line">            System.out.println(<span class="string">"t1线程继续执行..."</span>);</span><br><span class="line">          &#125; <span class="keyword">catch</span> (InterruptedException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">          &#125;</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;,<span class="string">"t1"</span>);</span><br><span class="line"></span><br><span class="line">      Thread t2 = <span class="keyword">new</span> Thread(<span class="keyword">new</span> Runnable() &#123;</span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">          <span class="keyword">try</span> &#123;</span><br><span class="line">            System.out.println(<span class="string">"t2线程进行初始化操作..."</span>);</span><br><span class="line">            Thread.sleep(<span class="number">3000</span>);</span><br><span class="line">            System.out.println(<span class="string">"t2线程初始化完毕，通知t1线程继续..."</span>);</span><br><span class="line">            countDown.countDown();</span><br><span class="line">          &#125; <span class="keyword">catch</span> (InterruptedException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">          &#125;</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;);</span><br><span class="line">      Thread t3 = <span class="keyword">new</span> Thread(<span class="keyword">new</span> Runnable() &#123;</span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">          <span class="keyword">try</span> &#123;</span><br><span class="line">            System.out.println(<span class="string">"t3线程进行初始化操作..."</span>);</span><br><span class="line">            Thread.sleep(<span class="number">4000</span>);</span><br><span class="line">            System.out.println(<span class="string">"t3线程初始化完毕，通知t1线程继续..."</span>);</span><br><span class="line">            countDown.countDown();</span><br><span class="line">          &#125; <span class="keyword">catch</span> (InterruptedException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">          &#125;</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;);</span><br><span class="line"></span><br><span class="line">      t1.start();</span><br><span class="line">      t2.start();</span><br><span class="line">      t3.start();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</code></pre><h6 id="执行结果"><a href="#执行结果" class="headerlink" title="执行结果 "></a>执行结果 <br></h6><pre><code><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">进入线程t1等待其他线程处理完成...</span><br><span class="line">t2线程进行初始化操作...</span><br><span class="line">t3线程进行初始化操作...</span><br><span class="line">t2线程初始化完毕，通知t1线程继续...</span><br><span class="line">t3线程初始化完毕，通知t1线程继续...</span><br><span class="line">t1线程继续执行...</span><br></pre></td></tr></table></figure>
</code></pre><h3 id="2-CylicBarrier-栅栏"><a href="#2-CylicBarrier-栅栏" class="headerlink" title="2. CylicBarrier(栅栏) "></a>2. CylicBarrier(栅栏) <br></h3><h5 id="CylicBarrier-的作用。"><a href="#CylicBarrier-的作用。" class="headerlink" title="CylicBarrier 的作用。"></a>CylicBarrier 的作用。<br></h5><pre><code> CyclicBarrier 的字面意思是可循环使用（Cyclic）的屏障（Barrier）。它要做的事情是，让一组线程到达一个屏障（也可以叫同步点）时被阻塞，直到最后一个线程到达屏障时，屏障才会开门，所有被屏障拦截的线程才会继续干活。&lt;br /&gt;
 CyclicBarrier默认的构造方法是CyclicBarrier(int parties)，其参数表示屏障拦截的线程数量，每个线程调用await方法告诉CyclicBarrier我已经到达了屏障，然后当前线程被阻塞。.&lt;br /&gt;
个人理解：CyclicBarrier:可看成是个障碍，所有的线程必须到齐后才能一起通过这个障碍.&lt;br /&gt;
</code></pre><h4 id="示例："><a href="#示例：" class="headerlink" title="示例："></a>示例：</h4><pre><code><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"> <span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyCyclicBarrier</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">   <span class="keyword">static</span> <span class="class"><span class="keyword">class</span> <span class="title">Runner</span> <span class="keyword">implements</span> <span class="title">Runnable</span> </span>&#123;  </span><br><span class="line">       <span class="keyword">private</span> CyclicBarrier barrier;  </span><br><span class="line">       <span class="keyword">private</span> String name;  </span><br><span class="line"></span><br><span class="line">       <span class="function"><span class="keyword">public</span> <span class="title">Runner</span><span class="params">(CyclicBarrier barrier, String name)</span> </span>&#123;  </span><br><span class="line">           <span class="keyword">this</span>.barrier = barrier;  </span><br><span class="line">           <span class="keyword">this</span>.name = name;  </span><br><span class="line">       &#125;  </span><br><span class="line">       <span class="meta">@Override</span>  </span><br><span class="line">       <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;  </span><br><span class="line">           <span class="keyword">try</span> &#123;  </span><br><span class="line">               Thread.sleep(<span class="number">1000</span> * (<span class="keyword">new</span> Random()).nextInt(<span class="number">5</span>));  </span><br><span class="line">               System.out.println(<span class="keyword">new</span> SimpleDateFormat(<span class="string">"YYYY-HH-MM hh:dd:ss"</span>).format(<span class="keyword">new</span> Date()) +<span class="string">" "</span> + name + <span class="string">" 准备OK."</span>);</span><br><span class="line">               barrier.await();  </span><br><span class="line">           &#125; <span class="keyword">catch</span> (InterruptedException e) &#123;  </span><br><span class="line">               e.printStackTrace();  </span><br><span class="line">           &#125; <span class="keyword">catch</span> (BrokenBarrierException e) &#123;  </span><br><span class="line">               e.printStackTrace();  </span><br><span class="line">           &#125;  </span><br><span class="line">           System.out.println(name + <span class="string">" Go!!"</span>);  </span><br><span class="line">       &#125;  </span><br><span class="line">   &#125;</span><br><span class="line"></span><br><span class="line">   <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException, InterruptedException </span>&#123;  </span><br><span class="line">       CyclicBarrier barrier = <span class="keyword">new</span> CyclicBarrier(<span class="number">3</span>);  <span class="comment">// 3</span></span><br><span class="line">       ExecutorService executor = Executors.newFixedThreadPool(<span class="number">3</span>);  </span><br><span class="line"></span><br><span class="line">       executor.submit(<span class="keyword">new</span> Thread(<span class="keyword">new</span> Runner(barrier, <span class="string">"zhangsan"</span>)));  </span><br><span class="line">       executor.submit(<span class="keyword">new</span> Thread(<span class="keyword">new</span> Runner(barrier, <span class="string">"lisi"</span>)));  </span><br><span class="line">       executor.submit(<span class="keyword">new</span> Thread(<span class="keyword">new</span> Runner(barrier, <span class="string">"wangwu"</span>)));  </span><br><span class="line"></span><br><span class="line">       executor.shutdown();  </span><br><span class="line">   &#125;  </span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

输出结果：
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">2018-02-06 02:30:06 lisi 准备OK.</span><br><span class="line">2018-02-06 02:30:06 zhangsan 准备OK.</span><br><span class="line">2018-02-06 02:30:07 wangwu 准备OK.</span><br><span class="line">2018-02-06 02:30:07 wangwu Go!!</span><br><span class="line">2018-02-06 02:30:07 lisi Go!!</span><br><span class="line">2018-02-06 02:30:07 zhangsan Go!!</span><br></pre></td></tr></table></figure>
</code></pre><h5 id="CyclicBarrier的应用场景"><a href="#CyclicBarrier的应用场景" class="headerlink" title="CyclicBarrier的应用场景"></a>CyclicBarrier的应用场景<br></h5><pre><code>CyclicBarrier可以用于多线程计算数据，最后合并计算结果的应用场景。&lt;br /&gt;
比如我们用一个Excel保存了用户所有银行流水，每个Sheet保存一个帐户近一年的每笔银行流水，现在需要统计用户的日均银行流水，先用多线程处理每个sheet里的银行流水，都执行完之后，得到每个sheet的日均银行流水，最后，再用barrierAction用这些线程的计算结果，计算出整个Excel的日均银行流水。&lt;br /&gt;
</code></pre><h3 id="3-Semaphore-信号量-简介"><a href="#3-Semaphore-信号量-简介" class="headerlink" title="3. Semaphore(信号量) 简介"></a>3. Semaphore(信号量) 简介<br></h3><h5 id="Semaphore的作用。"><a href="#Semaphore的作用。" class="headerlink" title="Semaphore的作用。"></a>Semaphore的作用。<br></h5><pre><code>在java中，使用了synchronized关键字和Lock锁实现了资源的并发访问控制，在同一时间只允许唯一了线程进入临界区访问资源(读锁除外)，这样子控制的主要目的是为了解决多个线程并发同一资源造成的数据不一致的问题。&lt;br /&gt;
 Semaphore主要是在多线程中可以轻松控制信号量，针对某个资源可被并发访问的个数。acquire（）方法可以或得一个访问的许可，release（）方法释放一个许可。提供同步机制，控制同时访问的个数。&lt;br /&gt;
</code></pre><h5 id="示例：-1"><a href="#示例：-1" class="headerlink" title="示例："></a>示例：</h5><pre><code><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MySemaphore</span> </span>&#123;  </span><br><span class="line">   <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;  </span><br><span class="line">     <span class="comment">// 线程池  </span></span><br><span class="line">     ExecutorService exec = Executors.newCachedThreadPool();  </span><br><span class="line">     <span class="comment">// 只能2个线程同时访问  </span></span><br><span class="line">     <span class="keyword">final</span> Semaphore semp = <span class="keyword">new</span> Semaphore(<span class="number">2</span>);  </span><br><span class="line">     <span class="comment">// 模拟10个客户端访问  </span></span><br><span class="line">     <span class="keyword">for</span> (<span class="keyword">int</span> index = <span class="number">0</span>; index &lt; <span class="number">10</span>; index++) &#123;  </span><br><span class="line">       <span class="keyword">final</span> <span class="keyword">int</span> NO = index;  </span><br><span class="line">       Runnable run = <span class="keyword">new</span> Runnable() &#123;  </span><br><span class="line">         <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;  </span><br><span class="line">           <span class="keyword">try</span> &#123;</span><br><span class="line">             <span class="comment">// 获取许可  </span></span><br><span class="line">             semp.acquire();  </span><br><span class="line">             System.out.println(<span class="keyword">new</span> SimpleDateFormat(<span class="string">"YYYY-HH-MM hh:dd:ss"</span>).format(<span class="keyword">new</span> Date()) +<span class="string">" "</span> + <span class="string">"Accessing: "</span> + NO);</span><br><span class="line">             <span class="comment">//模拟实际业务逻辑</span></span><br><span class="line">             Thread.sleep((<span class="keyword">long</span>) (Math.random() * <span class="number">10000</span>));  </span><br><span class="line">             <span class="comment">// 访问完后，释放  </span></span><br><span class="line">             semp.release();  </span><br><span class="line">           &#125; <span class="keyword">catch</span> (InterruptedException e) &#123;  </span><br><span class="line">           &#125;  </span><br><span class="line">         &#125;  </span><br><span class="line">       &#125;;  </span><br><span class="line">       exec.execute(run);  </span><br><span class="line">     &#125;</span><br><span class="line"></span><br><span class="line">     <span class="keyword">try</span> &#123;</span><br><span class="line">       Thread.sleep(<span class="number">10</span>);</span><br><span class="line">     &#125; <span class="keyword">catch</span> (InterruptedException e) &#123;</span><br><span class="line">       e.printStackTrace();</span><br><span class="line">     &#125;</span><br><span class="line"></span><br><span class="line">     <span class="comment">//System.out.println(semp.getQueueLength());</span></span><br><span class="line">     <span class="comment">// 退出线程池</span></span><br><span class="line">     exec.shutdown();  </span><br><span class="line">   &#125;</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>
</code></pre><h5 id="执行结果："><a href="#执行结果：" class="headerlink" title="执行结果："></a>执行结果：</h5>  <figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">2018-11-06 11:30:51 pool-1-thread-1接受服务: 0</span><br><span class="line">2018-11-06 11:30:51 pool-1-thread-2接受服务: 1</span><br><span class="line">2018-11-06 11:30:53 pool-1-thread-6接受服务: 5</span><br><span class="line">2018-11-06 11:30:56 pool-1-thread-3接受服务: 2</span><br><span class="line">2018-11-06 11:30:57 pool-1-thread-5接受服务: 4</span><br><span class="line">2018-11-06 11:30:58 pool-1-thread-10接受服务: 9</span><br><span class="line">2018-11-06 11:30:02 pool-1-thread-7接受服务: 6</span><br><span class="line">2018-11-06 11:30:04 pool-1-thread-8接受服务: 7</span><br><span class="line">2018-11-06 11:30:08 pool-1-thread-9接受服务: 8</span><br><span class="line">2018-11-06 11:30:10 pool-1-thread-4接受服务: 3</span><br></pre></td></tr></table></figure>
<h3 id="4-Future简介"><a href="#4-Future简介" class="headerlink" title="4. Future简介"></a>4. Future简介<br></h3><pre><code>在Java中一般通过继承Thread类或者实现Runnable接口这两种方式来创建多线程，但是这两种方式都有个缺陷，就是不能在执行完成后获取执行的结果，因此Java 1.5之后提供了Callable和Future接口，通过它们就可以在任务执行完毕之后得到任务的执行结果&lt;br /&gt;
Future接口代表异步计算的结果，通过Future接口提供的方法可以查看异步计算是否执行完成，或者等待执行结果并获取执行结果，同时还可以取消执行&lt;br /&gt;
FutureTask一个可取消的异步计算，FutureTask 实现了Future的基本方法，提空 start cancel 操作，可以查询计算是否已经完成，并且可以获取计算的结果。结果只可以在计算完成之后获取，get方法会阻塞当计算没有完成的时候，一旦计算已经完成，那么计算就不能再次启动或是取消。&lt;br /&gt;
FutureTask 可以用来包装一个 Callable 或是一个runnable对象。因为FurtureTask实现了Runnable方法，所以 FutureTask可以提交(submit)给Excutor执行(excution).&lt;br /&gt;
</code></pre><h5 id="FutureTask使用场景"><a href="#FutureTask使用场景" class="headerlink" title="FutureTask使用场景"></a>FutureTask使用场景</h5><pre><code>FutureTask可用于异步获取执行结果或取消执行任务的场景。通过传入Runnable或者Callable的任务给FutureTask，直接调用其run方法或者放入线程池执行，之后可以在外部通过FutureTask的get方法异步获取执行结果，因此，FutureTask非常适合用于耗时的计算，主线程可以在完成自己的任务后，再去获取结果。另外，FutureTask还可以确保即使调用了多次run方法，它都只会执行一次Runnable或者Callable任务，或者通过cancel取消FutureTask的执行等.&lt;br /&gt;
</code></pre><h4 id="示例：-2"><a href="#示例：-2" class="headerlink" title="示例："></a>示例：</h4><pre><code><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br></pre></td><td class="code"><pre><span class="line">  <span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyFuture</span> <span class="keyword">implements</span> <span class="title">Callable</span>&lt;<span class="title">String</span>&gt;</span>&#123;</span><br><span class="line">      <span class="keyword">private</span> String para;</span><br><span class="line"></span><br><span class="line">      <span class="function"><span class="keyword">public</span> <span class="title">MyFuture</span><span class="params">(String para)</span></span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.para = para;</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      <span class="comment">/**</span></span><br><span class="line"><span class="comment">       * 这里是真实的业务逻辑，其执行可能很慢</span></span><br><span class="line"><span class="comment">       */</span></span><br><span class="line">      <span class="meta">@Override</span></span><br><span class="line">      <span class="function"><span class="keyword">public</span> String <span class="title">call</span><span class="params">()</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        <span class="comment">//模拟执行耗时</span></span><br><span class="line">        Thread.sleep(<span class="number">5000</span>); <span class="comment">//模拟耗时业务</span></span><br><span class="line">        String result = <span class="keyword">this</span>.para + <span class="string">"处理完成"</span>;</span><br><span class="line">        <span class="keyword">return</span> result;</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      <span class="comment">//主控制函数</span></span><br><span class="line">      <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        String queryStr = <span class="string">"query"</span>;</span><br><span class="line">        <span class="comment">//构造FutureTask，并且传入需要真正进行业务逻辑处理的类,该类一定是实现了Callable接口的类</span></span><br><span class="line">        FutureTask&lt;String&gt; future = <span class="keyword">new</span> FutureTask&lt;String&gt;(<span class="keyword">new</span> MyFuture(queryStr));</span><br><span class="line"></span><br><span class="line">        FutureTask&lt;String&gt; future2 = <span class="keyword">new</span> FutureTask&lt;String&gt;(<span class="keyword">new</span> MyFuture(queryStr));</span><br><span class="line">        <span class="comment">//创建一个固定线程的线程池且线程数为1,</span></span><br><span class="line">        ExecutorService executor = Executors.newFixedThreadPool(<span class="number">2</span>);</span><br><span class="line">        <span class="comment">//这里提交任务future,则开启线程执行RealData的call()方法执行</span></span><br><span class="line">        <span class="comment">//submit和execute的区别： 第一点是submit可以传入实现Callable接口的实例对象， 第二点是submit方法有返回值</span></span><br><span class="line"></span><br><span class="line">        Future f1 = executor.submit(future);		<span class="comment">//单独启动一个线程去执行的</span></span><br><span class="line">        Future f2 = executor.submit(future2);</span><br><span class="line">        System.out.println(<span class="keyword">new</span> SimpleDateFormat(<span class="string">"YYYY-HH-MM hh:dd:ss"</span>).format(<span class="keyword">new</span> Date()) +<span class="string">" "</span> + <span class="string">"请求完毕"</span>);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">          <span class="comment">//这里可以做额外的数据操作，也就是主程序执行其他业务逻辑</span></span><br><span class="line">          System.out.println(<span class="keyword">new</span> SimpleDateFormat(<span class="string">"YYYY-HH-MM hh:dd:ss"</span>).format(<span class="keyword">new</span> Date()) +<span class="string">" "</span> + <span class="string">"处理实际的业务逻辑..."</span>);</span><br><span class="line">          Thread.sleep(<span class="number">1000</span>);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">          e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 可以取消异步任务</span></span><br><span class="line">        <span class="comment">//future2.cancel(true);</span></span><br><span class="line"></span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">          <span class="comment">// 阻塞，等待异步任务执行完毕-获取异步任务的返回值</span></span><br><span class="line">          <span class="comment">//调用获取数据方法,如果call()方法没有执行完成,则依然会进行等待</span></span><br><span class="line">          System.out.println(<span class="keyword">new</span> SimpleDateFormat(<span class="string">"YYYY-HH-MM hh:dd:ss"</span>).format(<span class="keyword">new</span> Date()) +<span class="string">" future1 "</span> + <span class="string">"数据："</span> + future.get());</span><br><span class="line">          System.out.println(<span class="keyword">new</span> SimpleDateFormat(<span class="string">"YYYY-HH-MM hh:dd:ss"</span>).format(<span class="keyword">new</span> Date()) +<span class="string">" future2"</span> + <span class="string">"数据："</span> + future2.get());</span><br><span class="line"></span><br><span class="line">        &#125; <span class="keyword">catch</span> (InterruptedException e) &#123;</span><br><span class="line">          e.printStackTrace();</span><br><span class="line">        &#125; <span class="keyword">catch</span> (ExecutionException e) &#123;</span><br><span class="line">          e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">        executor.shutdown();</span><br><span class="line">      &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">```  </span><br><span class="line">#### 运行结构：&lt;br /&gt;</span><br><span class="line">```json</span><br><span class="line">  <span class="number">2018</span>-<span class="number">11</span>-<span class="number">06</span> <span class="number">11</span>:<span class="number">30</span>:<span class="number">38</span> 请求完毕</span><br><span class="line">  <span class="number">2018</span>-<span class="number">11</span>-<span class="number">06</span> <span class="number">11</span>:<span class="number">30</span>:<span class="number">38</span> 处理实际的业务逻辑...</span><br><span class="line">  <span class="number">2018</span>-<span class="number">11</span>-<span class="number">06</span> <span class="number">11</span>:<span class="number">30</span>:<span class="number">39</span> future1 数据：query处理完成</span><br><span class="line">  <span class="number">2018</span>-<span class="number">11</span>-<span class="number">06</span> <span class="number">11</span>:<span class="number">30</span>:<span class="number">43</span> future2数据：query处理完成</span><br></pre></td></tr></table></figure>
</code></pre></the>
      
    </div>
    
  </div>
  
    
    <div class="copyright">
        <p><span>本文标题:</span><a href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/14.并发编程--Concurrent-工具类/">14.并发编程--Concurrent-工具类介绍</a></p>
        <p><span>文章作者:</span><a href="/" title="回到主页">zhengweiMao</a></p>
        <p><span>发布时间:</span>2018-07-05, 14:26:57</p>
        <p><span>最后更新:</span>2018-08-06, 23:26:04</p>
        <p>
            <span>原始链接:</span><a class="post-url" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/14.并发编程--Concurrent-工具类/" title="14.并发编程--Concurrent-工具类介绍">https://maozw.gitee.io/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/14.并发编程--Concurrent-工具类/</a>
            <span class="copy-path" data-clipboard-text="原文: https://maozw.gitee.io/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/14.并发编程--Concurrent-工具类/　　作者: zhengweiMao" title="点击复制文章链接"><i class="fa fa-clipboard"></i></span>
            <script> var clipboard = new Clipboard('.copy-path'); </script>
        </p>
        <p>
            <span>许可协议:</span><i class="fa fa-creative-commons"></i> <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/" title="CC BY-NC-SA 4.0 International" target = "_blank">"署名-非商用-相同方式共享 4.0"</a> 转载请保留原文链接及作者。
        </p>
    </div>



    <nav id="article-nav">
        
            <div id="article-nav-newer" class="article-nav-title">
                <a href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/15.并发编程--锁-介绍/">
                    15.并发编程--锁-介绍
                </a>
            </div>
        
        
            <div id="article-nav-older" class="article-nav-title">
                <a href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/13.并发编程--线程池介绍/">
                    13. 并发编程--线程池
                </a>
            </div>
        
    </nav>

  
</article>

    <div id="toc" class="toc-article">
        <strong class="toc-title">文章目录</strong>
        
            <ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#并发编程–Concurrent-工具类介绍"><span class="toc-number">1.</span> <span class="toc-text">并发编程–Concurrent-工具类介绍</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#并发编程–Concurrent-工具类介绍-1"><span class="toc-number">1.0.0.1.</span> <span class="toc-text">并发编程–Concurrent-工具类介绍</span></a></li></ol></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-CountDownLatch-计时器-简介"><span class="toc-number">2.</span> <span class="toc-text">1. CountDownLatch(计时器) 简介</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#CountDownLatch-的作用。"><span class="toc-number">2.0.1.</span> <span class="toc-text">CountDownLatch 的作用。</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#示例：MyCountDownLatch-Java"><span class="toc-number">2.1.</span> <span class="toc-text">示例：MyCountDownLatch.Java</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#执行结果"><span class="toc-number">2.1.0.1.</span> <span class="toc-text">执行结果 </span></a></li></ol></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-CylicBarrier-栅栏"><span class="toc-number">3.</span> <span class="toc-text">2. CylicBarrier(栅栏) </span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#CylicBarrier-的作用。"><span class="toc-number">3.0.1.</span> <span class="toc-text">CylicBarrier 的作用。</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#示例："><span class="toc-number">3.1.</span> <span class="toc-text">示例：</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#CyclicBarrier的应用场景"><span class="toc-number">3.1.1.</span> <span class="toc-text">CyclicBarrier的应用场景</span></a></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-Semaphore-信号量-简介"><span class="toc-number">4.</span> <span class="toc-text">3. Semaphore(信号量) 简介</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#Semaphore的作用。"><span class="toc-number">4.0.1.</span> <span class="toc-text">Semaphore的作用。</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#示例：-1"><span class="toc-number">4.0.2.</span> <span class="toc-text">示例：</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#执行结果："><span class="toc-number">4.0.3.</span> <span class="toc-text">执行结果：</span></a></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-Future简介"><span class="toc-number">5.</span> <span class="toc-text">4. Future简介</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#FutureTask使用场景"><span class="toc-number">5.0.1.</span> <span class="toc-text">FutureTask使用场景</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#示例：-2"><span class="toc-number">5.1.</span> <span class="toc-text">示例：</span></a></li></ol></li></ol>
        
    </div>
    <style>
        .left-col .switch-btn,
        .left-col .switch-area {
            display: none;
        }
        .toc-level-3 i,
        .toc-level-3 ol {
            display: none !important;
        }
    </style>

    <input type="button" id="tocButton" value="隐藏目录"  title="点击按钮隐藏或者显示文章目录">

    <script>
        yiliaConfig.toc = ["隐藏目录", "显示目录", !!"false"];
    </script>



    
<div class="share">
    
        <div class="bdsharebuttonbox">
            <a href="#" class="fa fa-twitter bds_twi" data-cmd="twi" title="分享到推特"></a>
            <a href="#" class="fa fa-weibo bds_tsina" data-cmd="tsina" title="分享到新浪微博"></a>
            <a href="#" class="fa fa-qq bds_sqq" data-cmd="sqq" title="分享给 QQ 好友"></a>
            <a href="#" class="fa fa-files-o bds_copy" data-cmd="copy" title="复制网址"></a>
            <a href="#" class="fa fa fa-envelope-o bds_mail" data-cmd="mail" title="通过邮件分享"></a>
            <a href="#" class="fa fa-weixin bds_weixin" data-cmd="weixin" title="生成文章二维码"></a>
            <a href="#" class="fa fa-share-alt bds_more" data-cmd="more"></i></a>
        </div>
        <script>
            window._bd_share_config={
                "common":{"bdSnsKey":{},"bdText":"14.并发编程--Concurrent-工具类介绍　| 花神　","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"24"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];
        </script>
    

    
</div>







    




    <div class="scroll" id="post-nav-button">
        
            <a href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/15.并发编程--锁-介绍/" title="上一篇: 15.并发编程--锁-介绍">
                <i class="fa fa-angle-left"></i>
            </a>
        

        <a title="文章列表"><i class="fa fa-bars"></i><i class="fa fa-times"></i></a>

        
            <a href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/13.并发编程--线程池介绍/" title="下一篇: 13. 并发编程--线程池">
                <i class="fa fa-angle-right"></i>
            </a>
        
    </div>

    <ul class="post-list"><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/08/05/花神(maoge)/9.Spring/SpringBoot/SpringBoot-启动源码解析/">1.SpringBoot源码分析之SpringBoot的启动过程</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/08/05/花神(maoge)/11.java-reflections/2.反射框架--reflections-抓取实例/">1.反射框架--reflections-抓取实例</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/08/05/花神(maoge)/11.服务器相关/1.服务器--Sigar 获取CPU和Memory内存等信息使用详解/">1.Sigar 获取CPU和Memory内存等信息使用详解</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/08/05/花神(maoge)/11.java-reflections/1.反射框架--reflections-基本介绍/">1.反射框架--reflections-基本介绍</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/08/05/花神(maoge)/10.算法/算法----外部排序/">2.算法 -外部排序</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/08/05/花神(maoge)/10.算法/算法----snowflake/">1.算法 -snowflake</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/08/05/花神(maoge)/8.面试/面试/">1.面试</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/08/01/花神(maoge)/6.quartz--定时任务框架/7.Quartz-Job状态与并发/">7.任务调度框架Quartz-Job状态与并发</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/08/01/花神(maoge)/6.quartz--定时任务框架/6.任务调度框架Quartz-config/">5.任务调度框架Quartz-Quartz-Job Stores</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/08/01/花神(maoge)/6.quartz--定时任务框架/5.任务调度框架Quartz-Job Stores/">5.任务调度框架Quartz-Quartz-Job Stores</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/08/01/花神(maoge)/6.quartz--定时任务框架/4.任务调度框架Quartz-SchedulerListeners/">4.任务调度框架Quartz-SchedulerListeners</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/08/01/花神(maoge)/6.quartz--定时任务框架/3.任务调度框架Quartz-TriggerListeners和JobListeners/">3.任务调度框架Quartz-Triggers-SimpleTrigger</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/08/01/花神(maoge)/6.quartz--定时任务框架/2-3.任务调度框架Quartz-Triggers-CronTrigger/">2-2.任务调度框架Quartz-Triggers-CronTrigger</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/08/01/花神(maoge)/6.quartz--定时任务框架/2-2.任务调度框架Quartz-Triggers-SimpleTrigger/">2-2.任务调度框架Quartz-Triggers-SimpleTrigger</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/08/01/花神(maoge)/6.quartz--定时任务框架/2-1.任务调度框架Quartz-Triggers详解理解/">2-1.任务调度框Quartz-Triggers详解理解</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/08/01/花神(maoge)/6.quartz--定时任务框架/1.任务调度框架Quartz-入门/">1.任务调度框架Quartz-入门</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/12/花神(maoge)/9.Spring/SpringMVC--源码解读/05.SpringMVC-----源码分析与实践--HandlerMapping(1)/">1.SpringMVC -源码分析与实践-HandlerMapping</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/12/花神(maoge)/9.Spring/SpringMVC--源码解读/04.SpringMVC-----源码分析与实践--DispatcherServlet核心/">4.SpringMVC -源码分析与实践-DispatcherServlet</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/12/花神(maoge)/9.Spring/SpringMVC--源码解读/03.SpringMVC-----源码分析与实践--初始化分析/">3.SpringMVC -初始化源码分析</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/12/花神(maoge)/9.Spring/SpringMVC--源码解读/02.SpringMVC-----源码分析与实践--环境搭建/">2.SpringMVC -源码分析与实践--环境搭建</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/12/花神(maoge)/7.JAVA_WEB相关技术/1.基于AsyncRestTemplate异步HTTP请求的一种轻量级技术实现/">1.基于AsyncRestTemplate异步HTTP请求的一种轻量级技术实</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/12/花神(maoge)/9.Spring/SpringMVC--源码解读/01.SpringMVC-----源码分析与实践----初步认知/">1.SpringMVC -源码分析与实践----初步认知</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/20.并发编程--并发框架-Disruptor--应用示例3/">20.并发编程--并发框架-Disruptor--应用示例3</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/19.并发编程--并发框架-Disruptor--应用示例2/">19并发编程--并发框架-Disruptor--应用示例2</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/18.并发编程--并发框架-Disruptor--应用示例1/">18.并发编程--并发框架-Disruptor--应用示例1</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/17.并发编程--并发框架-Disruptor--术语介绍/">17.并发编程--并发框架-Disruptor--介绍</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/16.并发编程--并发框架-Disruptor--入门介绍/">16.并发编程--并发框架-Disruptor--介绍</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/15.并发编程--锁-介绍/">15.并发编程--锁-介绍</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/14.并发编程--Concurrent-工具类/">14.并发编程--Concurrent-工具类介绍</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/13.并发编程--线程池介绍/">13. 并发编程--线程池</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/12.并发编程--多线程设计模式 - 生产者-消费者模式/">12并发编程\12.并发编程--多线程设计模式 - 生产者-消费者模式</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/11.并发编程--多线程设计模式 - Master-Worker模式/">11多线程设计模式 - Master-Worker模式</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/7.并发编程--单例模式(饥饿模式-懒汉模式)/">7.并发编程--单例模式(饥饿模式-懒汉模式)</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/10.并发编程--多线程设计模式 - Future模式/">10 多线程设计模式 - Future模式</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/8.并发编程--同步类容器和并发类容器/">8并发编程--同步类容器和并发类容器</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/6.并发编程--ThreadLocal/">6.并发编程--ThreadLocal</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/9.并发编程--Queue/">9并发编程--Queue</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/5.并发编程--多线程通信-wait-notify-模拟Queue/">并发编程--多线程通信-wait-notify-模拟Queue</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/4-2.并发编程--多线程通信-wait-notify-notifyAll-join/">4-2.并发编程--并发编程--多线程通信-wait-notify-notifyAll-join</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/4-1.并发编程--多线程通信-wait-notify/">4.并发编程--多线程通信-wait-notify</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/1.并发编程--线程基础/">并发编程--线程基础</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/3.并发编程--volatile 细节说明/">并发编程--volatile</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/2.并发编程-synchronized 细节说明/">并发编程-synchronized 细节说明</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/0.并发编程-readMe/">并发编程</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/05/05/花神(maoge)/2.网络编程/0.并发编程-readMe/">网络编程</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/05/05/花神(maoge)/2.网络编程/13.网络编程--Netty-文件的上传和下载/">13.网络编程--Netty-文件的上传和下载</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/05/05/花神(maoge)/2.网络编程/14.网络编程--mina-入门/">14.网络编程--mina-入门</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/05/05/花神(maoge)/2.网络编程/12.网络编程--Netty-实践-心跳检测/">12.网络编程--Netty-实践-心跳检测</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/05/05/花神(maoge)/2.网络编程/11.网络编程--Netty-实践-数据通信/">11.网络编程--Netty-实践-数据通信+心跳检测</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/05/05/花神(maoge)/2.网络编程/10.网络编程--Netty-基础-WebSocket/">10.网络编程--Netty-基础-WebSocket</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/05/05/花神(maoge)/2.网络编程/9.网络编程--Netty-基础-UDP/">9.网络编程--Netty-UDP</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/05/05/花神(maoge)/2.网络编程/8.网络编程--Netty-基础-拆包粘包-2/">8.网络编程--Netty-拆包粘包</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/05/05/花神(maoge)/2.网络编程/7.网络编程--Netty-基础-拆包粘包-1/">7.网络编程--Netty-拆包粘包</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/05/05/花神(maoge)/2.网络编程/6.网络编程--Netty-基础-HelloWorld/">6.网络编程--Netty-HelloWorld</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/05/05/花神(maoge)/2.网络编程/5.网络编程--Netty-基础-入门/">5.网络编程--Netty-入门</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/05/05/花神(maoge)/2.网络编程/4.网络编程--基础-AIO异步实现/">4.网络编程--基础-AIO异步实现</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/05/05/花神(maoge)/2.网络编程/3.网络编程--基础-NIO异步实现/">3.网络编程--基础-NIO异步实现</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/05/05/花神(maoge)/2.网络编程/2.网络编程--基础-BIO伪异步实现/">网络编程--基础</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/05/05/花神(maoge)/2.网络编程/1.网络编程--基础-socket/">网络编程--基础</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/04/05/花神(maoge)/5.分布式--zookeeper/2.zookeeper--集群搭建/">2.分布式--zookeeper\2.zookeeper--集群搭建</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/04/05/花神(maoge)/5.分布式--zookeeper/1.zookeeper--简介/">1.分布式--zookeeper--zookeeper简介</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/02/05/花神(maoge)/4.JVM相关/1.jvm虚拟机组成部分/">1.jvm虚拟机组成部分</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/02/05/花神(maoge)/3.缓存-中间件/Redis 配置文件说明/">Redis集群搭建-配置文件</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/02/05/花神(maoge)/3.缓存-中间件/Redis----集群搭建/">Redis集群搭建-实践</a></li></ul>




    <script>
        
    </script>
</div>
      <footer id="footer">
    <div class="outer">
        <div id="footer-info">
            <div class="footer-left">
                <i class="fa fa-copyright"></i> 
                2016-2018 zhengweiMao
            </div>
            <div class="footer-right">
                <a href="http://hexo.io/" target="_blank" title="快速、简洁且高效的博客框架">Hexo</a>  Theme <a href="https://github.com/MOxFIVE/hexo-theme-yelee" target="_blank" title="简而不减 Hexo 双栏博客主题  v3.5">Yelee</a> by MOxFIVE <i class="fa fa-heart animated infinite pulse"></i>
            </div>
        </div>
        
            <div class="visit">
                
                    <span id="busuanzi_container_site_pv" style='display:none'>
                        <span id="site-visit" title="本站到访数"><i class="fa fa-user" aria-hidden="true"></i><span id="busuanzi_value_site_uv"></span>
                        </span>
                    </span>
                
                
                    <span>| </span>
                
                
                    <span id="busuanzi_container_page_pv" style='display:none'>
                        <span id="page-visit"  title="本页阅读量"><i class="fa fa-eye animated infinite pulse" aria-hidden="true"></i><span id="busuanzi_value_page_pv"></span>
                        </span>
                    </span>
                
            </div>
        
    </div>
</footer>
    </div>
    
<script data-main="/maoge-blog/js/main.js" src="//cdn.bootcss.com/require.js/2.2.0/require.min.js"></script>

    <script>
        $(document).ready(function() {
            var iPad = window.navigator.userAgent.indexOf('iPad');
            if (iPad > -1 || $(".left-col").css("display") === "none") {
                var bgColorList = ["#9db3f4", "#414141", "#e5a859", "#f5dfc6", "#c084a0", "#847e72", "#cd8390", "#996731"];
                var bgColor = Math.ceil(Math.random() * (bgColorList.length - 1));
                $("body").css({"background-color": bgColorList[bgColor], "background-size": "cover"});
            }
            else {
                var backgroundnum = 5;
                var backgroundimg = "url(/background/bg-x.jpg)".replace(/x/gi, Math.ceil(Math.random() * backgroundnum));
                $("body").css({"background": backgroundimg, "background-attachment": "fixed", "background-size": "cover"});
            }
        })
    </script>





<div class="scroll" id="scroll">
    <a href="#" title="返回顶部"><i class="fa fa-arrow-up"></i></a>
    <a href="#comments" onclick="load$hide();" title="查看评论"><i class="fa fa-comments-o"></i></a>
    <a href="#footer" title="转到底部"><i class="fa fa-arrow-down"></i></a>
</div>
<script>
    // Open in New Window
    
        var oOpenInNew = {
            
            
            
            
            
            
             archives: ".archive-article-title", 
             miniArchives: "a.post-list-link", 
            
             friends: "#js-friends a", 
             socail: ".social a" 
        }
        for (var x in oOpenInNew) {
            $(oOpenInNew[x]).attr("target", "_blank");
        }
    
</script>

<script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js">
</script>
  </div>
</body>
</html>